﻿function closeForm() {
    if (confirm("mọi tác vụ trên form đều bị huỷ nếu bạn không submit \n Bạn có chắc chắn muốn đóng form này ?)")) {
        $('.form').stop(true, true).animate({ 'height': '0px' });
    }
    return false;
}
//--------Project Function---------------
function clearProjectForm() {
    $('#projectName').val("");
    $('.projectId').val("");
    $('#projectDescription').val("");
    $('.cke_editable').html("");
    $('#dNgay').val("");
    $('#projectImage').html("");
    $('#statusTrue').attr("checked",true);
    $('#projectCategory').html("");
    $('#projectForm').prop("action", "/Admin/ProjectCreate");
    $('#projectImageBox').html("");
    
}
function loadCategory() {
    $.get('/Admin/categoryGetSelectList', function (data) {
        $.each(data, function (index, element) {
            $('#projectCategory').append("<option value=\"" + element.Value + "\">" + element.Text + "</option>");
        });
    });
}

function loadProjectForm() {
    $.get("/Admin/FormProject", function (data) {
        $(".form").html(data);
    });
}
function openProjectForm() {
    clearProjectForm();
    loadCategory();
    $('.form').stop(true, true).animate({ 'height': '820px' });
}
function projectEdit(id) {
    loadProjectForm();
    openProjectForm();
    $.get('/Admin/projectGetById', { "id": id }, function (data) {
        $('#projectName').val(data.projectName);
        $('#projectDescription').val(data.projectDescription);
        $('#dNgay').val(data.projectDate);
        $('#sAnh').val(data.sAnh);
        $('.projectId').val(data.projectId);
        $('#projectCategory').val(data.categoryId);
        $('#projectForm').prop("action", "/Admin/ProjectEdit");
        if (data.status == true) $('#statusTrue').attr("checked", true);
        else $('#statusFalse').attr("checked", true);
        $('#projectImage').append("<img style=\"max-width:100px;height:auto;\" src=\"/Contents/Images/Project/" + data.sAnh + "\"/>");
        $.each(data.listImg, function (index, element) {
            $('#projectImageBox').append("<a href=\"#\" id=\"imagesProject"+index+"\" onclick=\"projectImagesDelete("+index+","+element.id+")\"><img src=\"/Contents/Images/Project/" + element.images + "\"/></a>");
        });
        CKEDITOR.replace('projectDescription', {
            fullPage: false, allowedContent: true, width: 400, height: 100, tabletools: 0
        });
    });
  
}
function projectCreate() {
    openProjectForm();
    CKEDITOR.replace('projectDescription', {
        fullPage: false, allowedContent: true, width: 400, height: 100, tabletools: 0
    });
}
function projectImagesDelete(index, id) {
    if (confirm("Bạn có chắc chắn muốn xoá ảnh này ?")) {
        $.post('/Admin/ProjectImagesDelete', { "id": id }, function (data) {
            if (data) {
                var imgid = "#imagesProject" + index;
                $(imgid).html("");
                alert("Xoa anh thanh cong !");
            }
        });
    }
    return false;
}
//--------News Function------------------
function clearNewsForm() {
    $('#sTieude').val("");
    $('#sTomtat').val("");
    $('#sNoidung').val("");
    $('#dDatepost').val("");
    $('#PK_iNewsID').val("");
    $('#statusTrue').attr("checked", true);
    $('#newsForm').prop("action", "/Admin/NewsCreate");
    $('#newsImages').html("");
}
function loadNewsCategory() {
    $.get('/Admin/getListCategoryNews', function (data) {
        $.each(data, function (index, element) {
            $('#newsCategory').append("<option value=\"" + element.Value + "\">" + element.Text + "</option>");
        });
    });
}
function openNewsForm() {
    clearNewsForm();
    loadNewsCategory();
    $('.form').stop(true, true).animate({ 'height': '680px' });
}
function newsEdit(id) {
    openNewsForm();
    $.get('/Admin/newsGetById', { "id": id }, function (data) {
        $('#sNoidung').val(data.sNoidung);
        $('#sTieude').val(data.sTieude);
        $('#sTomtat').val(data.sTomtat);
        $('.sDatepost').val(data.sDatepost);
        $('.PK_iNewsID').val(data.PK_iNewsID);
        $('#newsCategory').val(data.FK_iCategoryNewsID);
        $('.sAnh').val(data.sAnh);
        $('#newsForm').prop("action", "/Admin/NewsEdit");
        if (data.status == true) $('#statusTrue').attr("checked", true);
        else $('#statusFalse').attr("checked", true);
        $('#newsImages').append("<img style=\"height:100px;width:auto;\" src=\"/Contents/Images/News/" + data.sAnh + "\"/>");
    });
    CKEDITOR.replace('sNoidung', {
        fullPage: false, allowedContent: true, width: 400, height: 100, tabletools: 0
    });
}
function newsCreate() {
    openNewsForm();
    CKEDITOR.replace('sNoidung', {
        fullPage: false, allowedContent: true, width: 400, height: 100, tabletools: 0
    });
}
//--------Category Function--------------
function clearCategoryForm() {
    $('#sCategoryName').val("");
    $('#PK_iCategoryID').val("");
    $('#statusTrue').attr("checked", true);
    $('#categoryForm').prop("action", "/Admin/CategoryCreate");
}
function openCategoryForm() {
    clearCategoryForm();
    $('.form').stop(true, true).animate({ 'height': '180px' });
}
function categoryEdit(id) {
    openCategoryForm();
    $.get('/Admin/categoryGetById', { "id": id }, function (data) {
        $('#PK_iCategoryID').val(data.PK_iCategoryID);
        $('#sCategoryName').val(data.sCategoryName);
        $('#categoryForm').prop("action", "/Admin/CategoryEdit");
        if (data.status == true) $('#statusTrue').attr("checked", true);
        else $('#statusFalse').attr("checked", true);
    });

}
function categoryCreate() {
    openCategoryForm();
}
function categoryClearView() {
    $('.tablePost').html("");
    closePostForm();
}
function categoryView(id) {
    categoryClearView();
    $.get('/Admin/categoryGetById', { "id": id }, function (data) {
        var table = "<a href=\"#\" class=\"bt_red\" onclick=\"categoryClearView();\"><span class=\"bt_red_lft\"></span><strong>Close</strong><span class=\"bt_red_r\"></span></a>";
        table+="<h2>"+data.sCategoryName+"</h2>";
        table+="<table id=\"rounded-corner\" summary=\"2007 Major IT Companies' Profit\">";
        table+="<thead>";
        table+="<tr>";
        table+="     <th scope=\"col\" class=\"rounded\">Tiêu đề</th>";
        table+="     <th scope=\"col\" class=\"rounded\">Nội dung</th>";
        table+="     <th scope=\"col\" class=\"rounded\">Thứ tự hiển thị</th>";
        table+="     <th scope=\"col\" class=\"rounded\">Edit</th>";
        table+="     <th scope=\"col\" class=\"rounded-q4\">Delete</th>";
        table+="</tr>";
        table+="</thead>";
        table+="<tbody>";
        $.each(data.listPostCategory, function (index, element) {
            table+="<tr>";
            table+="<td>"+element.sTitle+"</td>";
            table+="<td>" + element.sDescription+ "</td>";
            table+="<td>"+element.iOrder +"</td>";
            table += "<td><a href=\"#\" onclick=\"postEdit("+element.PK_iPostID+");\">";
            table+="<img src=\"/Contents/Admin/Theme/images/user_edit.png\" border=\"0\" /></a></td>";
            table+="<td><a href=\"#\" onclick=\"postDelete("+element.PK_iPostID+")\" class=\"ask\">";
            table+="<img src=\"/Contents/Admin/Theme/images/trash.png\" border=\"0\" /></a></td>";
            table+="</tr>";
        });
        table+="</tbody>";
        table+="</table>";
        table += "<a href=\"#\" class=\"bt_green\" onclick=\"categoryPostCreate("+id+");\"><span class=\"bt_green_lft\"></span><strong>Add new item</strong><span class=\"bt_green_r\"></span></a>";
        $('.tablePost').append(table);
    });
}
function categoryPostCreate(id) {
    var form = "";
    form+="<a href=\"#\" class=\"bt_red\" onclick=\"closePostForm();\"><span class=\"bt_red_lft\"></span><strong>Close form</strong><span class=\"bt_red_r\"></span></a>";
    form += "<form action=\"/Admin/CategoryPostCreate\" method=\"post\" enctype=\"multipart/form-data\" id=\"postForm\">";
    form += "<input id=\"PK_iPostID\" name=\"PK_iPostID\" type=\"hidden\" value=\"\">";
    form += "<input id=\"FK_iCategoryID\" name=\"FK_iCategoryID\" type=\"hidden\" value=\"" + id + "\">";
    form += "<input id=\"sAnh\" name=\"sAnh\" type=\"hidden\" value=\"\">";
    form+=" <fieldset>";
    form+="     <dl>";
    form += "         <dt><label for=\"sTitle\">Tiêu đề:</label></dt>";
    form += "         <dd><input type=\"text\" name=\"sTitle\" id=\"sTitle\" size=\"40\" /></dd>";
    form += "     </dl>";
    form += "     <dl>";
    form += "         <dt><label for=\"sAnh\">Ảnh:</label></dt>";
    form += "         <dd><input type=\"file\" name=\"files\" id=\"upload\" /></dd>";
    form += "         <dd><img src=\"\" style=\"max-width:80px;height:auto;\" id=\"imagePostLink\" /></dd>";
    form += "     </dl>";
    form += "     <dl>";
    form += "         <dt><label for=\"iOrder\">Vị trí:</label></dt>";
    form += "         <dd><input type=\"text\" name=\"iOrder\" id=\"iOrder\" size=\"40\" /></dd>";
    form += "     </dl>";
    form += "     <dl>";
    form += "         <dt><label for=\"sDescription\">Nội dung:</label></dt>";
    form += "         <dd><textarea name=\"sDescription\" id=\"sDescription\" class=\"sDescription\" rows=\"5\" cols=\"36\"></textarea></dd>";
    form += "     </dl>";
    form+="     <dl class=\"submit\"><dd><input type=\"submit\" name=\"submit\" id=\"submit\" value=\"Submit\" /></dd></dl>";
    form+=" </fieldset>";
    form += "</form>";
    $('.postForm').html(form);
    CKEDITOR.replace('sDescription', {
        fullPage: false, allowedContent: true, width: 400, height: 100, tabletools: 0
    });
}
function closePostForm() {
    $('.postForm').html("");
}
$(document).on('submit', '#postForm', function (e) {
    var urlForm = $(this).attr("action");
    var dataForm = new FormData(this);
    $.ajax({
        url: urlForm,
        type: 'POST',
        data: dataForm,
        mimeType: "multipart/form-data",
        contentType: false,
        cache: false,
        processData: false,
        success: function (data) {
            alert(data);
            categoryView($('#FK_iCategoryID').val());
            closePostForm();
        },
        error: function (data) {
            alert("Lỗi: Vui lòng kiểm tra lại thông tin các mục đã điền ! \n Nếu tình trạng này vẫn sảy ra vui lòng báo với nhân viên kỹ thuật !");
        }
    });
    e.preventDefault();
    e.unbind();
});
function postDelete(id) {
    if (confirm("Bạn có chắc chắn muốn xoá mục này ?")) {
        $.post('/Admin/PostDelete', { "id": id }, function (data) {
            categoryView(data);
        });
    }
}
function postEdit(id) {
    categoryPostCreate(0);
    $.get('/Admin/getPostById', { "id": id }, function (data) {
        $('#PK_iPostID').val(data.PK_iPostID);
        $('#FK_iCategoryID').val(data.FK_iCategoryID);
        $('#sAnh').val(data.sAnh);
        $('#sTitle').val(data.sTitle);
        $('#iOrder').val(data.iOrder);
        $('#sDescription').val(data.sDescription);
        $('#imagePostLink').prop("src", "/Contents/Images/Post/" + data.sAnh);
        $('#postForm').prop("action", "/Admin/CategoryPostEdit");
    });
}
